package cn.tannn.tcache.command.string;

import cn.tannn.tcache.core.Command;
import cn.tannn.tcache.core.Reply;
import cn.tannn.tcache.core.Tcache;

/**
 * 递增命令
 * <p> incr k1
 *
 * @author <a href="https://t.tannn.cn/">tnnn</a>
 * @version V1.0
 * @date 2024/6/19 下午8:52
 */
public class IncrCommand implements Command {
    @Override
    public String name() {
        return "INCR";
    }

    @Override
    public Reply<?> exec(Tcache cache, String[] args) {
        try {
            return Reply.integer(cache.incr(getKey(args)));
        } catch (NumberFormatException nfe) {
            String key = getKey(args);
            return Reply.error("NFE " + key + " value[" + cache.get(key) + "] is not an integer.");
        }
    }
}
